package it.gameplanet.client.widgets;

import it.gameplanet.client.GamePlanetContext;
import it.gameplanet.model.User;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class UserPanel extends Composite {

	private final static String LOGIN = DOM.getElementById("login").getPropertyString("value");
	private final static String LOGOUT = DOM.getElementById("logout").getPropertyString("value");
	
	
	public UserPanel() {
		super();

		initWidget(createWidget());
	}

	private final Widget createWidget() {
		
		final VerticalPanel container = new VerticalPanel();
		
		User user = GamePlanetContext.getUser();
		
		if(user==null) {
			Hyperlink login = new Hyperlink("Ciao, clicca qui per la login", "login");
			container.add(login);
			
			login.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					Window.Location.assign(LOGIN);
				}
				
			});
		}
		else {
			Hyperlink logout = new Hyperlink("Ciao "+user.getNickname()+", clicca qui per uscire", "logout");
			container.add(logout);
			
			logout.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					Window.Location.assign(LOGOUT);
				}
			});								
		}
		
		return container;
	}
}
